﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FD6 RID: 4054
	internal class CombineMeshUtility
	{
		// Token: 0x0600D268 RID: 53864 RVA: 0x00311A40 File Offset: 0x0030FC40
		public CombineMeshUtility()
		{
			for (int i = 0; i < CombineMeshUtility.MaxPartCount; i++)
			{
				this.matCombineInstanceArrayCache.Add(new CombineInstance[i + 1]);
			}
		}

		// Token: 0x0600D269 RID: 53865 RVA: 0x00311A8C File Offset: 0x0030FC8C
		private CombineInstance[] GetMatCombineInstanceArray(int partCount)
		{
			int num = partCount - 1;
			CombineInstance[] result;
			if (num >= 0)
			{
				if (num >= this.matCombineInstanceArrayCache.Count)
				{
					result = new CombineInstance[partCount];
				}
				else
				{
					result = this.matCombineInstanceArrayCache[num];
				}
			}
			else
			{
				result = null;
			}
			return result;
		}

		// Token: 0x0600D26A RID: 53866 RVA: 0x00311AE0 File Offset: 0x0030FCE0
		public bool Combine(CombineMeshTask combineTask)
		{
			int num = 0;
			for (int i = 0; i < 8; i++)
			{
				PartLoadTask partLoadTask = combineTask.parts[i] as PartLoadTask;
				if (partLoadTask.HasMesh())
				{
					num++;
				}
			}
			combineTask.isOnepart = false;
			CombineInstance[] matCombineInstanceArray = this.GetMatCombineInstanceArray(num);
			bool result;
			if (matCombineInstanceArray != null && combineTask.skin != null)
			{
				bool flag3 = false;
				PartLoadTask partLoadTask2 = null;
				int num2 = 0;
				for (int j = 0; j < 8; j++)
				{
					PartLoadTask partLoadTask3 = combineTask.parts[j] as PartLoadTask;
					if (partLoadTask3.HasMesh())
					{
						if (partLoadTask3.mesh.isReadable)
						{
							CombineInstance combineInstance = default(CombineInstance);
							combineInstance.mesh = partLoadTask3.mesh;
							if (partLoadTask3.partType != XMeshPartList.NormalPart)
							{
								combineTask.isOnepart = true;
								flag3 = (partLoadTask3.partType == XMeshPartList.CutoutPart);
								partLoadTask2 = partLoadTask3;
							}
							combineInstance.subMeshIndex = 0;
							combineInstance.subMeshIndex = 0;
							matCombineInstanceArray[num2++] = combineInstance;
						}
						else
						{
							XSingleton<XDebug>.singleton.AddErrorLog2("Mesh not readable:{0}", new object[]
							{
								j
							});
						}
					}
				}
				if (combineTask.skin.sharedMesh == null)
				{
					combineTask.skin.sharedMesh = new Mesh();
					combineTask.skin.sharedMesh.MarkDynamic();
				}
				else
				{
					combineTask.skin.sharedMesh.Clear(true);
				}
				combineTask.skin.enabled = false;
				combineTask.skin.gameObject.SetActive(false);
				combineTask.skin.sharedMesh.CombineMeshes(matCombineInstanceArray, true, false);
				combineTask.skin.gameObject.SetActive(true);
				if (combineTask.skin.sharedMaterial != null)
				{
					XEquipDocument.ReturnMaterial(combineTask.skin.sharedMaterial);
					combineTask.skin.sharedMaterial = null;
				}
				if (combineTask.skin.sharedMaterial == null)
				{
					if (CombineMeshTask.s_CombineMatType == ECombineMatType.ECombined)
					{
						combineTask.skin.sharedMaterial = XEquipDocument.GetRoleMat(combineTask.isOnepart, flag3, 0);
					}
					else
					{
						combineTask.skin.sharedMaterial = XEquipDocument.GetRoleMat(combineTask.isOnepart, flag3, combineTask.roleType);
					}
				}
				if (combineTask.skin.sharedMaterial != null)
				{
					combineTask.skin.GetPropertyBlock(combineTask.mpb);
					if (combineTask.isOnepart)
					{
						PartLoadTask partLoadTask4 = combineTask.parts[XFastEnumIntEqualityComparer<EPartType>.ToInt(EPartType.ECombinePartStart)] as PartLoadTask;
						PartLoadTask partLoadTask5 = combineTask.parts[XFastEnumIntEqualityComparer<EPartType>.ToInt(EPartType.EHair)] as PartLoadTask;
						PartLoadTask partLoadTask6 = combineTask.parts[XFastEnumIntEqualityComparer<EPartType>.ToInt(EPartType.EHeadgear)] as PartLoadTask;
						Texture texture = partLoadTask5.GetTexture();
						ShaderManager.SetTexture(combineTask.mpb, partLoadTask4.GetTexture(), ShaderManager._ShaderKeyIDFace);
						if (texture == null)
						{
							ShaderManager.SetTexture(combineTask.mpb, partLoadTask6.GetTexture(), ShaderManager._ShaderKeyIDHair);
						}
						else
						{
							ShaderManager.SetTexture(combineTask.mpb, texture, ShaderManager._ShaderKeyIDHair);
						}
						if (partLoadTask2 != null)
						{
							ShaderManager.SetTexture(combineTask.mpb, partLoadTask2.GetTexture(), ShaderManager._ShaderKeyIDBody);
							if (flag3)
							{
								ShaderManager.SetTexture(combineTask.mpb, partLoadTask2.GetAlpha(), ShaderManager._ShaderKeyIDAlpha);
							}
						}
					}
					else
					{
						int k = 0;
						int num3 = XFastEnumIntEqualityComparer<EPartType>.ToInt(EPartType.ECombinePartEnd);
						while (k < num3)
						{
							PartLoadTask partLoadTask7 = combineTask.parts[k] as PartLoadTask;
							if (partLoadTask7.HasMesh())
							{
								ShaderManager.SetTexture(combineTask.mpb, partLoadTask7.GetTexture(), ShaderManager._ShaderKeyIDSkin[k]);
							}
							k++;
						}
					}
				}
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}

		// Token: 0x04005F97 RID: 24471
		public static int MaxPartCount = 8;

		// Token: 0x04005F98 RID: 24472
		private List<CombineInstance[]> matCombineInstanceArrayCache = new List<CombineInstance[]>();
	}
}
